package top.yuki.data;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.yaml.snakeyaml.Yaml;
import top.yuki.LevelWorld;

import java.io.*;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;

import static org.bukkit.Bukkit.getLogger;


public class WorldManage {
    private LevelWorld lw;
    public Configuration config;
    public File dataFolder = null;
    public File dataFile = null;
    public HashMap<String,HashMap> worldMap;
    public static WorldManage init(LevelWorld lw) {

        return new WorldManage(lw);
    }
    public WorldManage(LevelWorld lw){
        this.lw = lw;
        this.dataFolder = new File(lw.getDataFolder()+File.separator+"worldData");
        this.dataFile = new File(dataFolder+File.separator+"worldData.yml");
        this.config = lw.getConfig();
        if (!dataFolder.exists() ) {
            dataFolder.mkdirs();
        }

        if (!dataFile.exists()){
            try {
                dataFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.worldMap = loadData();
        autoDistributionWorldLevel();
    }
    private void autoDistributionWorldLevel(){
        for (World world : Bukkit.getWorlds()) {
            String name = world.getName();
            if (!worldMap.containsKey(name)){
                setWorld(world.getUID(),world.getName(),0);
            }
        }

    }
    private HashMap loadData(){


        Yaml yml = new Yaml();
        try {
            InputStream reader = new FileInputStream(dataFile);
            HashMap map = yml.loadAs(reader, HashMap.class);
            if (map==null){
                return new HashMap();
            }
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    public void save(){


        Yaml yaml = new Yaml();
        try {
            FileWriter writer = new FileWriter(dataFile);
            yaml.dump(worldMap,writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getWorldLevel(String name){
        int level = 0;

        if (worldMap.containsKey(name)){
            level = (int) worldMap.get(name).get("level");

        }

        return level;

    }
    public void changeUUID(String name,UUID uuid){
        if (worldMap.containsKey(name)) {
            worldMap.get(name).replace("uuid",uuid);
        }
    }
    public void changeLevel(String name,int level){
        if (worldMap.containsKey(name)) {
            worldMap.get(name).replace("level",level);
        }
    }
    public boolean existWorld(String name){
        if (worldMap.containsKey(name)) {
            return true;
        }else{
            return false;
        }

    }
    public boolean setWorld(UUID uuid, String name, int level){



            HashMap hashMap = new HashMap();
            hashMap.put("level",level);
            hashMap.put("uuid",uuid);

            worldMap.put(name,hashMap);
        File file = new File(dataFolder + File.separator + level);
        if (!file.exists()){
            file.mkdir();
        }
            save();
        return true;


    }

    public void generate() {
        boolean generate = (boolean) config.get("firstStart");
        int maxWorld = (int) config.get("maxWorld");

        if (generate) {
            for (int i = 1; i <= maxWorld; i++) {
                getLogger().info("生成世界"+i+"设置死亡不掉落，设置火焰不蔓延");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv create world_"+i+" normal");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv create nether_"+i+" nether");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv create end_"+i+" end");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv create ziyuan_"+i+" normal");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mvnp link nether world_"+ i +" nether_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mvnp link end world_"+ i +" end_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule keepInventory true world_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule keepInventory true nether_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule keepInventory true end_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule keepInventory true ziyuan_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule doFireTick false world_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule doFireTick false nether_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule doFireTick false end_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"mv gamerule doFireTick false ziyuan_"+i);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"wb world_"+i+" set 3000 0 0");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"wb nether_"+i+" set 2000 0 0");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"wb end_"+i+" set 1000 0 0");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"wb ziyuan_"+i+" set 3000 0 0");


                setWorld(Bukkit.getWorld("world_"+i).getUID(),"world_"+i,i);
                setWorld(Bukkit.getWorld("nether_"+i).getUID(),"nether_"+i,i);
                setWorld(Bukkit.getWorld("end_"+i).getUID(),"end_"+i,i);
                setWorld(Bukkit.getWorld("ziyuan_"+i).getUID(),"ziyuan_"+i,i);

            }
            config.set("firstStrat",false);

        }
    }
}
